<?php

namespace app\admin\controller;

use app\admin\model\Api;
use app\admin\model\Column;
use app\admin\model\Member;
use app\admin\model\MemberLevel;

class MemberLevelController extends BaseController
{
    public function index()
    {
        return $this->fetch();
    }
    public function read()
    {
        $Member = new MemberLevel();
        if (isset($_GET['type']) && $_GET['type']  == 'tree'){
            $data =(new Api())->select();//
            $checked = $this->request->get("checked");
            foreach ($data as $key=>$item) {
                if (in_array($item['api_id'], explode(',', $checked))){
                    $data[$key]['checked'] =true;
                }else{
                    $data[$key]['checked'] =false;
                }
            }
            $alias = ['api_name' => 'name', 'api_parent' => 'pid', 'api_id' => 'value','checked'=>'checked'];
            $tree_list = tree($data, 'api_id', $alias,'list');
            return json(['code'=>0,'msg'=>'数据返回成功','data'=>$tree_list]) ;
        }
        $data = $Member->select();
        return json(array('code' => 0, 'msg' => "", 'data' => $data, 'count' =>$Member->count()));
    }

    public function  del(){
        $result =MemberLevel::get(input('post.'))->delete();
        if($result){
            return json(['code'=>0,'msg'=>'删除成功']);
        }else{
            return json(['code'=>500,'msg'=>'删除失败']);
        }
    }
    public function upd(){
        $Member = (new MemberLevel())->isUpdate(true)->save(input('post.'));
        if ($Member){
            return  json(['code'=>0,'msg'=>'更新成功']);
        }else{
            return  json(['code'=>0,'msg'=>'删除成功']);
        }
    }
    public function add(){
       $res =  (new MemberLevel())->save(input('post.'));
       if ($res){
           return json(['code'=>0,'msg'=>'添加成功']);
       }else{
           return json(['code'=>500,'msg'=>'添加失败']);
       }
    }
}
